package com.zdl.interestingapp.view

import android.app.Activity

/**
 * @author zdl
 * @date 2018/4/20 16:18
 * email zdl328465042@163.com
 * description 方便快捷实现退出整个APP
 */
object ActivityCollector {

    /**
     * 新建一个list来暂存activity
     */
    private var activities = ArrayList<Activity>()

    /**
     * 向list中添加activity
     * @param activity 要添加的activity
     */
    fun addActivity(activity: Activity): Unit {
        activities.add(activity)
    }

    /**
     * 从list中移除activity
     * @param activity 要移除的activity
     */
    fun removeActivity(activity: Activity): Unit {
        activities.remove(activity)
    }

    fun exitAPP(): Unit {
        for (activity in activities){
            //判断activity是否存在，若存在，则销毁该activity
            if (!activity.isFinishing){
                activity.finish()
            }
        }
        //销毁全部activity后清空activities
        activities.clear()
        //杀死当前进程
        android.os.Process.killProcess(android.os.Process.myPid())
    }
}